package com.zk.yygh.hosp.controller.api;

import com.alibaba.excel.util.StringUtils;
import com.zk.yygh.helper.HttpRequestHelper;
import com.zk.yygh.hosp.exception.HospitalException;
import com.zk.yygh.hosp.result.Result;
import com.zk.yygh.hosp.result.ResultCodeEnum;
import com.zk.yygh.hosp.service.DepartmentService;
import com.zk.yygh.hosp.service.HospitalService;
import com.zk.yygh.hosp.service.HospitalSetService;
import com.zk.yygh.hosp.service.ScheduleService;
import com.zk.yygh.model.hosp.Department;
import com.zk.yygh.model.hosp.Hospital;
import com.zk.yygh.model.hosp.Schedule;
import com.zk.yygh.util.MD5;
import com.zk.yygh.vo.hosp.DepartmentQueryVo;
import com.zk.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 对接医院方
 * @author: 张凯
 * @date: 2022/6/3 - 21:25
 */
@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    //医院的service
    private HospitalService hospitalService;
    @Autowired
    //科室的service
    private DepartmentService departmentService;
    @Autowired
    //医院设置的service
    private HospitalSetService hospitalSetService;
    @Autowired
    //科室信息Service
    private ScheduleService scheduleService;

    @ApiOperation("上传医院")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        System.out.println("----->医院上传接口");
        //1.对象转换
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(request.getParameterMap());
        System.out.println(parameterMap);
        //2.参数校验
        String hoscode = (String) parameterMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            //抛出自定义的参数不正确异常
            throw new HospitalException(ResultCodeEnum.PARAM_ERROR);
        }
        //3.签名校验
        String sign = hospitalSetService.getSignKey(hoscode);
        //从平台方查出来的签名进行md5加密
        String signKeySys = MD5.encrypt(sign);
        System.out.println("---->平台方：" + signKeySys);
        //获取医院方的签名
        Object signKeyHosp = parameterMap.get("sign");
        System.out.println("---->医院方：" + signKeyHosp);
        System.out.println(signKeyHosp.equals(signKeySys));
        if (!signKeyHosp.equals(signKeySys)) {
            //抛出签名错误异常
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        //4.base64中图片传输中+变为了空格->要转换过来
        String logoData = (String) parameterMap.get("logoData");
        String s = logoData.replaceAll(" ", "+");
        parameterMap.put("logoData", s);
        //5.调用业务层实现上传
        hospitalService.save(parameterMap);
        return Result.ok();
    }

    @ApiOperation("查询医院")
    @PostMapping("hospital/show")
    public Result hospitalShow(HttpServletRequest request) {
        //1.对象转换
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //2.参数校验
        String hoscode = (String) parameterMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            //抛出自定义的参数不正确异常
            throw new HospitalException(ResultCodeEnum.PARAM_ERROR);
        }
        //3.签名校验
        String sign = hospitalSetService.getSignKey(hoscode);
        //从平台方查出来的签名进行md5加密
        String signKeySys = MD5.encrypt(sign);
        System.out.println("---->平台方：" + signKeySys);
        //获取医院方的签名
        Object signKeyHosp = parameterMap.get("sign");
        System.out.println("---->医院方：" + signKeyHosp);
        System.out.println(signKeyHosp.equals(signKeySys));
        if (!signKeyHosp.equals(signKeySys)) {
            //抛出签名错误异常
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        //4.执行查询
        Hospital hospital = hospitalService.getHospitalByHoscode(hoscode);
        //5.返回结果
        return Result.ok(hospital);
    }

    @ApiOperation("上传科室")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        //1.对象转换
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //2.参数校验
        String hoscode = (String) parameterMap.get("hoscode");
        String depcode = (String) parameterMap.get("depcode");
        if (StringUtils.isEmpty(hoscode) || StringUtils.isEmpty(depcode)) {
            //抛出自定义的参数不正确异常
            throw new HospitalException(ResultCodeEnum.PARAM_ERROR);
        }
        //3.签名校验
        String sign = hospitalSetService.getSignKey(hoscode);
        //从平台方查出来的签名进行md5加密
        String signKeySys = MD5.encrypt(sign);
        //获取医院方的签名
        String signKeyHosp = (String) parameterMap.get("sign");
        if (!signKeyHosp.equals(signKeySys)) {
            //抛出签名错误异常
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        //4.调用业务层执行上传科室
        departmentService.save(parameterMap);
        return Result.ok();
    }

    @ApiOperation("查询科室")
    @PostMapping("department/list")
    public Result selectDepartmentList(HttpServletRequest request) {
        //1.对象转换
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //2.参数校验
        String hoscode = (String) parameterMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            //抛出自定义的参数不正确异常
            throw new HospitalException(ResultCodeEnum.PARAM_ERROR);
        }
        //3.签名校验
        String sign = hospitalSetService.getSignKey(hoscode);
        //从平台方查出来的签名进行md5加密
        String signKeySys = MD5.encrypt(sign);
        //获取医院方的签名
        String signKeyHosp = (String) parameterMap.get("sign");
        if (!signKeyHosp.equals(signKeySys)) {
            //抛出签名错误异常
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        //4.获取相关参数及创建对象
        int page = StringUtils.isEmpty(parameterMap.get("page")) ? 1 : Integer.parseInt((String) parameterMap.get("page"));
        int limit = StringUtils.isEmpty(parameterMap.get("limit")) ? 10 : Integer.parseInt((String) parameterMap.get("limit"));
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);  //设想：按Hoscode进行查询
        //调用业务层进行分页查询
        Page<Department> pages = departmentService.selectPage(page, limit, departmentQueryVo);
        return Result.ok(pages);
    }

    @ApiOperation("删除科室")
    @PostMapping("department/remove")
    public Result departmentRemove(HttpServletRequest request) {
        //1.对象转换
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //2.参数校验
        String hoscode = (String) parameterMap.get("hoscode");
        String depcode = (String) parameterMap.get("depcode");
        if (StringUtils.isEmpty(hoscode) || StringUtils.isEmpty(depcode)) {
            //抛出自定义的参数不正确异常
            throw new HospitalException(ResultCodeEnum.PARAM_ERROR);
        }
        //3.签名校验
        String sign = hospitalSetService.getSignKey(hoscode);
        //从平台方查出来的签名进行md5加密
        String signKeySys = MD5.encrypt(sign);
        //获取医院方的签名
        String signKeyHosp = (String) parameterMap.get("sign");
        if (!signKeyHosp.equals(signKeySys)) {
            //抛出签名错误异常
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        //4.调用业务层执行删除
        departmentService.remove(hoscode, depcode);
        //5.返回结果
        return Result.ok();
    }

    @ApiOperation("上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        //1.对象转换
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //2.参数校验
        String hoscode = (String) parameterMap.get("hoscode");
        String hosScheduleId = (String) parameterMap.get("hosScheduleId");
        if (StringUtils.isEmpty(hoscode) || StringUtils.isEmpty(hosScheduleId)) {
            //抛出自定义的参数不正确异常
            throw new HospitalException(ResultCodeEnum.PARAM_ERROR);
        }
        //3.签名校验
        String sign = hospitalSetService.getSignKey(hoscode);
        //从平台方查出来的签名进行md5加密
        String signKeySys = MD5.encrypt(sign);
        //获取医院方的签名
        String signKeyHosp = (String) parameterMap.get("sign");
        if (!signKeyHosp.equals(signKeySys)) {
            //抛出签名错误异常
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        //4.执行上传操作
        scheduleService.save(parameterMap);
        //5.返回结果
        return Result.ok();
    }

    @ApiOperation("查询排班")
    @PostMapping("schedule/list")
    public Result scheduleList(HttpServletRequest request) {
        //1.对象转换
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //2.参数校验
        String hoscode = (String) parameterMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            //抛出自定义的参数不正确异常
            throw new HospitalException(ResultCodeEnum.PARAM_ERROR);
        }
        //3.签名校验
        String sign = hospitalSetService.getSignKey(hoscode);
        //从平台方查出来的签名进行md5加密
        String signKeySys = MD5.encrypt(sign);
        //获取医院方的签名
        String signKeyHosp = (String) parameterMap.get("sign");
        if (!signKeyHosp.equals(signKeySys)) {
            //抛出签名错误异常
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        //4.获取相关参数
        int page=StringUtils.isEmpty(parameterMap.get("page"))?1:Integer.parseInt((String) parameterMap.get("page"));
        int limit = StringUtils.isEmpty(parameterMap.get("limit")) ? 10 : Integer.parseInt((String) parameterMap.get("limit"));
        ScheduleQueryVo scheduleQueryVo=new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        //5.调用业务层执行查询
        Page<Schedule> pages = scheduleService.selectPage(page, limit, scheduleQueryVo);
        //6.返回查询结果
        return Result.ok(pages);
    }

    @ApiOperation("删除排班")
    @PostMapping("schedule/remove")
    public Result scheduleRemove(HttpServletRequest request){
        //1.对象转换
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //2.参数校验
        String hoscode = (String) parameterMap.get("hoscode");
        String hosScheduleId = (String) parameterMap.get("hosScheduleId");
        if (StringUtils.isEmpty(hoscode) || StringUtils.isEmpty(hosScheduleId)) {
            //抛出自定义的参数不正确异常
            throw new HospitalException(ResultCodeEnum.PARAM_ERROR);
        }
        //3.签名校验
        String sign = hospitalSetService.getSignKey(hoscode);
        //从平台方查出来的签名进行md5加密
        String signKeySys = MD5.encrypt(sign);
        //获取医院方的签名
        String signKeyHosp = (String) parameterMap.get("sign");
        if (!signKeyHosp.equals(signKeySys)) {
            //抛出签名错误异常
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        //4.调用业务层执行删除操作
        scheduleService.remove(parameterMap);
        //5.返回结果
        return Result.ok();
    }
}
